package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.curriculum.questionbank.QuestionChangeType;
import com.edu.exam.enums.curriculum.questionbank.QuestionApprovalStatus;
import com.edu.exam.enums.curriculum.questionbank.ReviewStatus;
import com.edu.exam.entity.curriculum.classification.QuestionType;
import com.edu.exam.enums.exam.QuestionStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 试题历史实体类
 * 对应数据库表：question_histories
 * 
 * 管理试题的历史变更记录，包括变更类型、操作人、操作时间等。
 * 支持试题历史的查询和跟踪。
 * 记录变更内容、版本信息等关键信息，提供试题历史变更的完整视图。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class QuestionHistory {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull(message = "试题ID不能为空")
    @Column(name = "question_id", nullable = false)
    private Long questionId;

    @Column(name = "question_code", length = 50)
    private String questionCode;

    @Enumerated(EnumType.STRING)
    @Column(name = "question_type", length = 30)
    private QuestionType questionType;

    @NotNull(message = "变更类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "change_type", nullable = false, length = 30)
    private QuestionChangeType changeType;

    @NotNull(message = "变更时间不能为空")
    @CreatedDate
    @Column(name = "change_time", nullable = false)
    private LocalDateTime changeTime;

    @Column(name = "version")
    private Integer version;

    @Size(max = 50, message = "操作人长度不能超过50个字符")
    @Column(name = "operator", length = 50)
    private String operator;

    @Column(name = "operator_role", length = 50)
    private String operatorRole;

    @Size(max = 1000, message = "变更原因长度不能超过1000个字符")
    @Column(name = "change_reason", length = 1000)
    private String changeReason;

    @Size(max = 2000, message = "变更描述长度不能超过2000个字符")
    @Column(name = "change_description", length = 2000)
    private String changeDescription;

    @Column(name = "old_content", columnDefinition = "TEXT")
    private String oldContent;

    @Column(name = "new_content", columnDefinition = "TEXT")
    private String newContent;

    @Column(name = "content_diff", columnDefinition = "TEXT")
    private String contentDiff;

    @Column(name = "changed_fields", length = 1000)
    private String changedFields;

    @Column(name = "old_difficulty_level")
    private Integer oldDifficultyLevel;

    @Column(name = "new_difficulty_level")
    private Integer newDifficultyLevel;

    @Column(name = "old_points")
    private java.math.BigDecimal oldPoints;

    @Column(name = "new_points")
    private java.math.BigDecimal newPoints;

    @Enumerated(EnumType.STRING)
    @Column(name = "old_status", length = 20)
    private QuestionStatus oldStatus;

    @Enumerated(EnumType.STRING)
    @Column(name = "new_status", length = 20)
    private QuestionStatus newStatus;

    @Column(name = "old_question_bank_id")
    private Long oldQuestionBankId;

    @Column(name = "new_question_bank_id")
    private Long newQuestionBankId;

    @Column(name = "old_question_bank_name", length = 200)
    private String oldQuestionBankName;

    @Column(name = "new_question_bank_name", length = 200)
    private String newQuestionBankName;

    @Column(name = "review_required")
    @Builder.Default
    private Boolean reviewRequired = false;

    @Enumerated(EnumType.STRING)
    @Column(name = "review_status", length = 20)
    private ReviewStatus reviewStatus;

    @Column(name = "reviewer", length = 50)
    private String reviewer;

    @Column(name = "review_time")
    private LocalDateTime reviewTime;

    @Size(max = 1000, message = "审核意见长度不能超过1000个字符")
    @Column(name = "review_comments", length = 1000)
    private String reviewComments;

    @Column(name = "approval_required")
    @Builder.Default
    private Boolean approvalRequired = false;

    @Enumerated(EnumType.STRING)
    @Column(name = "approval_status", length = 20)
    private QuestionApprovalStatus approvalStatus;

    @Column(name = "approver", length = 50)
    private String approver;

    @Column(name = "approval_time")
    private LocalDateTime approvalTime;

    @Size(max = 1000, message = "审批意见长度不能超过1000个字符")
    @Column(name = "approval_comments", length = 1000)
    private String approvalComments;

    @Column(name = "is_reverted")
    @Builder.Default
    private Boolean isReverted = false;

    @Column(name = "reverted_by", length = 50)
    private String revertedBy;

    @Column(name = "reverted_time")
    private LocalDateTime revertedTime;

    @Size(max = 1000, message = "回滚原因长度不能超过1000个字符")
    @Column(name = "revert_reason", length = 1000)
    private String revertReason;

    @Column(name = "reverted_history_id")
    private Long revertedHistoryId;

    @Column(name = "is_automated")
    @Builder.Default
    private Boolean isAutomated = false;

    @Column(name = "automation_source", length = 100)
    private String automationSource;

    @Column(name = "batch_operation_id", length = 50)
    private String batchOperationId;

    @Column(name = "import_session_id", length = 50)
    private String importSessionId;

    @Column(name = "export_session_id", length = 50)
    private String exportSessionId;

    @Column(name = "data_migration_id", length = 50)
    private String dataMigrationId;

    @Column(name = "system_event", length = 100)
    private String systemEvent;

    @Column(name = "external_reference", length = 100)
    private String externalReference;

    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    @Size(max = 1000, message = "备注信息长度不能超过1000个字符")
    @Column(name = "remarks", length = 1000)
    private String remarks;

    @Column(name = "ip_address", length = 50)
    private String ipAddress;

    @Column(name = "user_agent", length = 500)
    private String userAgent;

    @Column(name = "session_id", length = 100)
    private String sessionId;

    // 关联关系
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "question_id", insertable = false, updatable = false)
    private Question question;

}